﻿using System;
using System.Linq;

namespace FlowersStore
{
	public partial class OrderSummary : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (Session["order"] != null)
			{
				UserOrder order = (UserOrder)Session["order"];
				lblSenderName.Text = string.Format("Имя: {0}", order.senderName);
				lblSenderPhone.Text = string.Format("Телефоны: {0}", order.senderPhone);
				lblSenderaddInfo.Text = string.Format("Доп. информация: {0}", order.senderAddInfo);

				lblReceiverAddInfo.Text = string.Format("Доп. информация: {0}", order.receiverAddInfo);
				lblReceiverName.Text = string.Format("Имя: {0}", order.senderName);
				lblReceiverPhone.Text = string.Format("Телефоны: {0}", order.receiverPhone);

				lblPrice.Text = string.Format("{0:n0}", order.TotalPrice);
				lblDeliveryTime.Text = string.Format("Время: {0:d} {1:t} - {2:t}", order.deliveryDate, order.startTime, order.endTime);
				lblAddress.Text = string.Format("Адрес доставки: {0}", order.address);
				lblAdd.Text = order.addInfo;
				string prname;
				using(FlowerStoreDataContext db =new FlowerStoreDataContext())
					foreach (var pos in order.Positions)
					{
						prname = (from p in db.Products
								  where p.Product_id == pos.ProductId
								  select p.Product_name).Single();
						lbOrderProducts.Items.Add(string.Format("{0}: {1} шт.", prname, pos.Quantity));
					}
				((FlowersStore.SiteMaster)this.Master).FillCartItems();
			}
			else
				Response.Redirect("default.aspx");
		}

		protected void btnConfirm_Click(object sender, EventArgs e)
		{			
            if (!Page.IsValid)
            {
                lblerror.Visible = true;
                return;
            }
		    if (Session["order"] == null) return;
		    UserOrder order = (UserOrder)Session["order"];
		    using (FlowerStoreDataContext db = new FlowerStoreDataContext())
		    {
		        Person senderPerson = new Person();
		        senderPerson.Person_name = order.senderName;
		        senderPerson.Person_phone = order.senderPhone;
		        senderPerson.Person_additionalInfo = order.senderAddInfo;
		        db.Persons.InsertOnSubmit(senderPerson);

		        Person receiverPerson = new Person();
		        receiverPerson.Person_name = order.receiverName;
		        receiverPerson.Person_phone = order.receiverPhone;
		        receiverPerson.Person_additionalInfo = order.receiverAddInfo;
		        db.Persons.InsertOnSubmit(receiverPerson);
		        db.SubmitChanges();
					
		        Order ord = new Order();
		        ord.Order_deliveryDT = order.deliveryDate;
		        ord.Order_deliveryEndTime = order.endTime;
		        ord.Order_deliveryStartTime = order.startTime;
		        ord.Order_address = order.address;
		        ord.Order_additionalInfo = order.addInfo;
		        ord.OrderStatus_id = 2;
		        ord.Sender_id = senderPerson.Person_id;
		        ord.Receiver_id = receiverPerson.Person_id;
		        db.Orders.InsertOnSubmit(ord);
		        db.SubmitChanges();

		        foreach (var pos in order.Positions)
		        {
		            OrderDetail od = new OrderDetail();
		            od.Order_id = ord.Order_id;
		            od.OrderDetails_quantity = pos.Quantity;
		            od.Product_id = pos.ProductId;
		            db.OrderDetails.InsertOnSubmit(od);
		        }
		        db.SubmitChanges();
		        order.id = ord.Order_id;
		        Session["order"] = order;
		    }
		    Response.Redirect("Payment.aspx");
		}
	}
}